package com.cloud.common.domain;

import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;

/**
 * 统一返回对象
 *
 * @author yzj
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class Result<T> implements Serializable, Cloneable {

    private static final long serialVersionUID = 1L;
    public static final Integer SUCCESS = 1;
    // 失败异常
    public static final Integer ERROR = 0;
    // 警示异常
    public static final Integer WARNING = 10;
    private Integer code;
    private String message;
    private T data;

    public Result() {
        super();
    }

    /**
     * 返回提示信息
     *
     * @param code    状态码
     * @param message 状态信息
     * @param data    返回数据
     * @return
     */
    private static <T> Result<T> restResult(Integer code, String message, T data) {
        Result<T> res = new Result<>();
        res.setCode(code);
        res.setMessage(message);
        res.setData(data);
        return res;
    }

    public static <T> Result<T> data(Integer code, String message, T data) {
        return restResult(code, message, data);
    }

    public static <T> Result<T> message(Integer code, String message) {
        return restResult(code, message, null);
    }

    public static <T> Result<T> message(String message, T data) {
        return restResult(WARNING, message, data);
    }

    public static <T> Result<T> message(String message) {
        return restResult(WARNING, message, null);
    }

    public static <T> Result<T> error(String message) {
        return restResult(ERROR, message, null);
    }

    public static <T> Result<T> success(String message, T data) {
        return restResult(SUCCESS, message, data);
    }

    public static <T> Result<T> success(String message) {
        return restResult(SUCCESS, message, null);
    }

}
